﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.Properties;
using System.IO;
using Client.View.Forms;
using Client.View;
using System.Reflection;

namespace Client
{
    public partial class LoginView : Form, ILoginView
    {
        #region Private fields
        
        /// <summary>
        /// Icon, displayed when login failed
        /// </summary>
        private Bitmap m_errorIcon;

        /// <summary>
        /// Width and height of error icon
        /// </summary>
        private const int m_iconSize = 32;

        #endregion Private fields

        #region Private methods

        /// <summary>
        /// Load error icon from resources and set it size
        /// </summary>
        private void M_MakeErrorIcon()
        {
            //get icon from resources
            Stream iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Client.Resources.error.ico");
            Icon tmpIcon = new Icon(iconStream);

            //Convert Icon to Bitmap and set size 32x32
            m_errorIcon = new Bitmap(m_iconSize, m_iconSize);

            using (Graphics g = Graphics.FromImage((Image)m_errorIcon))
            {
                ///make size 32x32
                g.DrawImage(tmpIcon.ToBitmap(), 0, 0, m_iconSize, m_iconSize);
            }
        }

        /// <summary>
        /// Clear error icon after some time
        /// </summary>
        private void M_HideErrorIcon()
        {
            pictureBox1.Image = null;
            pictureBox1.Invalidate();       
        }

        #endregion Private methods


        #region Constructor

        public LoginView()
        {
            InitializeComponent();
            
            ///Load error icon from resources and set it size
            M_MakeErrorIcon();            

            ///create login presenter 
            LoginPresenter loginPresenter = new LoginPresenter(this);
        }

        #endregion Constructor


        #region Events

        //our custom event. it fires on clicking btnLogin 
        public event EventHandler LoginClick;

        #endregion Events


        #region Event Handlers

        /// <summary>
        /// Click Login button
        /// </summary>     
        private void btnLogin_Click(object sender, EventArgs e)
        {                    
            //create parameters to give them to the presenter
        /*    LoginEventArgs ea = new LoginEventArgs();
            ea.Login = this.Login;
            ea.Password = this.Password;  

            //fire up LoginClick event
            if (this.LoginClick != null)
            {
                this.LoginClick(this, ea);
            }*/
            MainWindow main = new MainWindow();
            main.Controls.Add(new Client.View.Components.OrdersView());
            main.Show();
        }

        /// <summary>
        /// When login text is changed, hide error icon 
        /// </summary>
        private void txtLogin_TextChanged(object sender, EventArgs e)
        {
            M_HideErrorIcon();
        }

        /// <summary>
        /// When password text is changed, hide error icon 
        /// </summary>
        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            M_HideErrorIcon();
        }

        #endregion Event Handlers         

       
        #region Public Properties

        /// <summary>
        /// Get login from TextBox
        /// </summary>
        public string Login
        {
            get { return txtLogin.Text; }
        }

        /// <summary>
        /// Get password from TextBox
        /// </summary>
        public string Password
        {
            get { return txtPassword.Text; }
        }

        #endregion Public Properties


        #region public Methods

        /// <summary>
        /// Show icon when login failed
        /// </summary>
        public void ShowErrorIcon()
        {
            pictureBox1.Image = m_errorIcon;
        }

        /// <summary>
        /// Close login form
        /// </summary>
        public void CloseWindow()
        {
            this.Visible = false;
        }

        #endregion public Methods      

       
    }//class
}
